<template>
  <div class="info-dialog">
    <el-dialog
      :visible.sync="visible"
      :title="title"
      :modal-append-to-body="true"
      :append-to-body="true"
      :destroy-on-close="false"
      :modal="true"
      :close-on-click-modal="false"
      top="10vh"
      width="550px"
      :fullscreen="true"
      @closed="closed"
    >
      <el-form ref="formData" :model="formData" :rules="rules">

        <el-form-item label-width="100px" label="项目ID" prop="name">
          <el-input v-model="formData.id" placeholder="" autocomplete="off" readonly="" />
        </el-form-item>

        <el-form-item label-width="100px" label="项目名称" prop="name">
          <el-input v-model="formData.name" placeholder="" autocomplete="off" />
        </el-form-item>

        <el-form-item label-width="100px" label="项目路径" prop="exeFilePath">
          <el-input v-model="formData.exeFilePath" placeholder="请输入项目路径" autocomplete="off" />
        </el-form-item>

        <el-form-item label-width="100px" label="帧率" prop="frameRate">
          <el-input placeholder="请输入内容" v-model="formData.frameRate" type="number" min=24 max=60>
            <template slot="append">FPS</template>
          </el-input>
        </el-form-item>

        <el-form-item label-width="100px" label="预设分辨率" prop="resX">
          <el-input-number v-model="formData.resX" :min="200" :max="7680" label="请输入序号" style="margin-right: 5px;" />
          <el-input-number v-model="formData.resY" :min="200" :max="7680" label="请输入序号" />
        </el-form-item>

        <el-form-item label-width="100px" label="备注" prop="desc">
          <el-input v-model="formData.desc" type="textarea" placeholder="请输入备注信息(250字符内)." />
        </el-form-item>

      </el-form>
      <div style="text-align: right;">
        <el-button @click="closed" :loading="loading">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    saveProject
  } from '@/api/cloud/project'
  export default {
    name: 'EditingPlatformDialog',
    components: {
    },
    props: {
      properties: {
        type: Object,
        default: () => ({})
      }
    },
    computed:{

    },
    watch:{
      properties: {
        handler: function(value) {
          this.visible = value.visible
          if (value.data) {
            this.formData.id = value.data.id || ''
            this.formData.name = value.data.name || ''
            this.formData.frameRate = value.data.frameRate || 24
            this.formData.desc = value.data.desc || ''
            this.formData.exeFilePath = value.data.exeFilePath || ''
            this.formData.resX = value.data.resX || 200
            this.formData.resY = value.data.resX || 200

          } else {
            this.formData.id =''
            this.formData.name = ''
            this.formData.frameRate = 24
            this.formData.desc = ''
            this.formData.exeFilePath =''
            this.formData.resX =200
            this.formData.resY =200
          }
          if (value.type) {
            this.type = value.type
            if (value.type === '0') {
              this.title = '新增'
            }
            if (value.type === '1') {
              this.title = '编辑'
            }
          }
        },
        immediate: true,
        deep: false
      }
    },
    data(){
      return {
        visible:false,
        title:'',
        loading:false,
        formData: {
          id: '',
          name: '',
          frameRate:24,
          exeFilePath:'',
          desc: '',
          resX:200,
          resY:200,
        },
        rules: {
          name: [{
            required: true,
            message: '请输入项目名称',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '长度在 1 到 50 个字符',
            trigger: 'blur'
          }
          ],
          exeFilePath: [{
            required: true,
            message: '请输入项目路径.',
            trigger: 'blur'
          }
          ],
          frameRate: [{
            required: true,
            message: '请输入帧率.',
            trigger: 'blur'
          }
          ],
          resX: [{
            required: true,
            message: '请输入预设分辨率.',
            trigger: 'blur'
          }
          ],
          desc: [{
              min: 0,
              max: 250,
              message: '长度在 0 到 250 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods:{
      submitForm: function() {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            this.loading = true
            saveProject(this.formData).then(rsp => {
              this.$message({
                type: 'success',
                message:rsp.message
              })
              this.closed()
            }).catch(err => {
              console.log(err);
            })
            .finally(() => {
              this.loading = false
            })
          }
        })
      },
      closed: function() {
        this.$emit('closed', false)
      },
    }
  }

</script>

<style>
</style>
